package com.study.mike.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.study.mike.model.dto.PageDTO;
import com.study.mike.model.dto.SysLogDTO;
import com.study.mike.model.po.SysLogPO;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.SysOperLogService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author KangHui
 * @Date 2023/7/6 14:22
 */
@RestController
@RequestMapping("sys/log")
public class SysOperLogController {
    @Resource
    private SysOperLogService logService;


    /**
     * 分页获取系统日志
     *
     * @param po
     * @return
     */
    @PostMapping("/query")
    public R<PageDTO<SysLogDTO>> query(@RequestBody SysLogPO po) {
        return RUtil.success(logService.query(po));
    }

    /**
     * 编辑
     *
     * @param po
     * @return
     */
    @PostMapping("/edit")
    public R edit(@RequestBody SysLogPO po) {
        if (ObjectUtil.isNull(po.getId())) {
            return RUtil.fail("请选择操作数");
        }
        Boolean edit = logService.edit(po);
        if (edit) {
            return RUtil.success("操作成功");
        }
        return RUtil.fail("更新失败");
    }

    /**
     * 批量删除数据
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return RUtil.fail("请选择删除数据");
        }
        Boolean flag = logService.deleteBatch(ids);
        if (flag) {
            return RUtil.success("操作成功");
        }
        return RUtil.fail("删除失败");
    }
}
